// const Page = require('../../utils/ald-stat.js').Page;
import regeneratorRuntime from '../../plugins/regenerator-runtime.js'
import { Request_API_CN, Request_API_COM } from '../../utils/index.js'//封装请求
const app = getApp()
Component({
  data:{
    data: [],//图集数据
    commentLayer: false,//是否展开评论详情
    inputvalue: '',//评论内容
    screen: true,//是否全屏展示图集层
    commentData: [],//图集层评论列表
    topHeight: 0,//scroll距离顶部位置
    isThumbsUp: false,//点赞高亮
    isCollection: false,//收藏高亮
    thumbsUp: null,//点赞次数
    page:1,//分页
    startY:0,
    moveY:0,
    load:false,//页面是否加载完成
  },
  properties:{
    ID: String
  },
  ready: function (e) {
    this.AtlasData(this.data.ID);//图集数据
    this.commentList();//图集评论
    this.atlasGate();//图集最后一页跳转小程序
  },
  methods:{
    //bindchange() { this.setData({ screen: false }) },//图集模块swiper滚动触发
    //screenSwiper() { this.setData({ screen: !this.data.screen }) },//是否全屏展示图集层
    atlasSwiper() { this.triggerEvent('isAtlas', { isAtlas: false }); wx.showTabBar() },//关闭图集层 
    bindLayer() { this.setData({ commentLayer: true }) },//展开评论详情 
    layerClose() { this.setData({ commentLayer: false }) },//隐藏评论详情 
    bindInput(e) { this.setData({ inputvalue: e.detail.value }); },//评论输入
    AtlasData(id) {//图集数据
      Request_API_CN('/psychological_test/getAtlas', 'POST', { id: id, openid: app.globalData.openid, appid: 22 })
        .then((res) => {
          // console.log(res.data.data)
          this.setData({
            data: res.data.data.desc,
            thumbsUp: res.data.data.thumbsUp,
            isThumbsUp: res.data.data.isThumbsUp,
            isCollection: res.data.data.isCollection,
            load:true
          })
          
          wx.hideLoading()
        })
    },
    formSubmit(e){ //模板消息
      let openid = app.globalData.openid;
      let formId = e.detail.formId;
      let data = {
        openid: openid,
        fid: formId,
        tablename: 'user22'
      }
      Request_API_CN('/currency/getfid', 'POST', data)
    },
    dtap() {//点赞
      Request_API_CN('/psychological_test/thumbs_up', 'POST', { id: this.data.ID, openid: app.globalData.openid, appid: 22 })
        .then((res) => {
          wx.showToast({ title: res.data.message, icon: 'none' })
          _this.AtlasData(this.data.ID)
        })
    },
    stap (){//收藏
      Request_API_CN('/psychological_test/collection', 'POST', { id: this.data.ID, openid: app.globalData.openid, appid: 22 })
        .then((res) => {
          wx.showToast({ title: res.data.message, icon: 'none' })
          this.AtlasData(this.data.ID)
        })
    },
    commentList(){//图集评论列表
      Request_API_CN('/psychological_test/commentList', 'POST', { id: this.data.ID, page: 1, openid: app.globalData.openid })
      .then((res)=>{
        this.setData({
          commentData:res.data.data
        })
        // console.log(this.data.commentData)
      })
    },
    bindConfirm(){//评论
      Request_API_CN('/psychological_test/comment', 'POST', { id: this.data.ID, openid: app.globalData.openid, content: this.data.inputvalue }).then((res) => {
        // console.log(res)
        this.commentList()
        this.setData({ inputvalue: '' })//清空input
      })
    },
    isClick(e){//评论点赞
      let ID = e.currentTarget.dataset.id;
      wx.showLoading({ mask: true })
      Request_API_CN('/psychological_test/commentThumbsUp', 'POST', { id: ID, openid: app.globalData.openid })
        .then((res) => {
          this.data.commentData.map((item, index) => {
            if (e.currentTarget.dataset.id == item.id && !item.isThumbsUp) {
              item.isThumbsUp = true;
              item.thumbsUpNum = item.thumbsUpNum + 1
            }
          })
          this.setData({ commentData: this.data.commentData })
          wx.hideLoading()
        })
    },
    atlasGate(){//最后一页跳转小程序
      Request_API_CN('/psychological_test/atlasBanner', 'POST', {})
      .then((res)=>{
        this.setData({
          atlasGate: res.data.data
        })
      })
    },
    preventD() { },
    bindscrolltolower(e){//图集评论scroll滚动到底部
      wx.showLoading({ mask: true });
      this.data.page++;
      Request_API_CN('/psychological_test/commentList', 'POST', { id: this.data.ID, page: this.data.page, openid: app.globalData.openid })
        .then((res) => {
         this.setData({ commentData: this.data.commentData.concat(res.data.data) })
          wx.hideLoading()
        })
    },
    bindtouchstart(e){//手指触摸开始
      console.log("手指触摸开始"+e.changedTouches[0].pageY)
      this.setData({
        startY: e.changedTouches[0].pageY
      })
    },
    bindtouchmove(e){//手指触摸移动
      let Config = 70;//超过距离隐藏
      let B = e.changedTouches[0].pageY - this.data.startY;//下滚
      let C = this.data.startY - e.changedTouches[0].pageY;//上滚
      let Y = e.changedTouches[0].pageY;
      let MoveY = this.data.moveY;

      this.setData({ moveY: e.changedTouches[0].pageY })


      if (MoveY < Y || Y < MoveY){

        console.log("手指触摸移动" + e.changedTouches[0].pageY)
        
        if (B>=Config||C>=Config){
          this.atlasSwiper()
          this.empty()
          console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
        }else{

          console.log('**************************************')

        }

      }else{
        console.log('禁止回滚')
      }


    },
    bindtouchend(){//手指触摸结束
      this.empty()
    },
    empty(){//清空Y轴坐标
      this.setData({
        startY:0,
        moveY:0
      })
    },
    previewImg(e){//点击预览图片
      let url = e.currentTarget.dataset.url;
      wx.previewImage({
        urls: [url]
      })

    }
  }
})